// exports.ServerBackup = void 0;
// var AL_GAME_HUA_GameConst = require("./AL_GAME_HUA_GameConst");
// var AL_GAME_HUA_BankData = require("./AL_GAME_HUA_BankData");
// var AL_GAME_HUA_CurrencyData = require("./AL_GAME_HUA_CurrencyData");
// var AL_GAME_HUA_DateData = require("./AL_GAME_HUA_DateData");
// var AL_GAME_HUA_EmailData = require("./AL_GAME_HUA_EmailData");
// var AL_GAME_HUA_GameData = require("./AL_GAME_HUA_GameData");
// var AL_GAME_HUA_MissionData = require("./AL_GAME_HUA_MissionData");
// var AL_GAME_HUA_OnlineGiftData = require("./AL_GAME_HUA_OnlineGiftData");
// var AL_GAME_HUA_PayData = require("./AL_GAME_HUA_PayData");
// var AL_GAME_HUA_PlayerInfo = require("./AL_GAME_HUA_PlayerInfo");
// var AL_GAME_HUA_ShopData = require("./AL_GAME_HUA_ShopData");
// var AL_GAME_HUA_WelfareData = require("./AL_GAME_HUA_WelfareData");
// var AL_GAME_HUA_Define = require("./AL_GAME_HUA_Define");
// var AL_GAME_HUA_Instance = require("./AL_GAME_HUA_Instance");
// (function (e) {
//     function t() {
//         var e = AL_GAME_HUA_GameData.GameData.getInstance().huoqu_yijiesuo_guanqia();
//         var t = AL_GAME_HUA_GameConst.GameConst.getInstance().backup_level;
//         var n = AL_GAME_HUA_GameConst.GameConst.getInstance().backup_space;
//         console.log(e, t, n);
//         return e > t
//             ? AL_GAME_HUA_Instance.Instance.LevelType == AL_GAME_HUA_Define.Define.LevelType.NORMAL
//                 ? (e - 1 - t) % n == 0 || (Log.d("===jiancha_shifou_xuyao_tijiao_cundang:", "level % backupSpace != 0", e, t, n), !1)
//                 : AL_GAME_HUA_Instance.Instance.LevelType == AL_GAME_HUA_Define.Define.LevelType.SPECIAL &&
//                   (((e = AL_GAME_HUA_GameData.GameData.getInstance().huoqu_yijiesuo_teshu_guanqia()) - 1) % n == 0 ||
//                       (Log.d("===jiancha_shifou_xuyao_tijiao_cundang:", "specialLevel % backupSpace != 0", e, n), !1))
//             : (Log.d("===jiancha_shifou_xuyao_tijiao_cundang:", "level <= backupLevel", e, t), !1);
//     }

//     function n(e) {
//         if (void 0 === e) {
//             e = !1;
//         }
//         return new Promise(function (n, o) {
//             if ((e || t()) && ailin_sdk_manager.beifenYouxiShuju) {
//                 ailin_sdk_manager.beifenYouxiShuju({
//                     callFun: function (e) {
//                         200 == e.code
//                             ? (console.log("===提交成功：" + JSON.stringify(e)), n())
//                             : (console.log("===提交失败：" + JSON.stringify(e)), o());
//                     }
//                 });
//             }
//         });
//     }

//     function y() {
//         var e = [];
//         var t = 0;
//         for (var n = m(); t < n.length; t++) {
//             var i = n[t];
//             e = __spreadArrays(e, i.huoqu_yuancheng_key_list());
//         }
//         return __spreadArrays([ailin.Key_Gold], e);
//     }

//     function m() {
//         return [
//             AL_GAME_HUA_BankData.BankData.getInstance(),
//             AL_GAME_HUA_CurrencyData.CurrencyData.getInstance(),
//             AL_GAME_HUA_DateData.DateData.getInstance(),
//             AL_GAME_HUA_EmailData.EmailData.getInstance(),
//             AL_GAME_HUA_GameData.GameData.getInstance(),
//             AL_GAME_HUA_MissionData.MissionData.getInstance(),
//             AL_GAME_HUA_OnlineGiftData.OnlineGiftData.getInstance(),
//             AL_GAME_HUA_PayData.PayData.getInstance(),
//             AL_GAME_HUA_PlayerInfo.PlayerInfo.getInstance(),
//             AL_GAME_HUA_ShopData.ShopData.getInstance(),
//             AL_GAME_HUA_WelfareData.WelfareData.getInstance()
//         ];
//     }
//     e.jiancha_shifou_xuyao_tijiao_cundang = t;
//     e.zhixing_shifou_tijiao_cundang = n;
//     e.zhixing_suoyou_cundang_de_caozuo = function () {
//         !(function (e) {
//             var t = user_default.get_int_for_key("al_Online_Data_Version");
//             var n = e.term();
//             if (0 == t && n) {
//                 var o = 0;
//                 for (var i = e.keys; o < i.length; o++) {
//                     var r = i[o];
//                     var a = cc.sys.localStorage.getItem(r);
//                     if ("string" == typeof a && ailin_sdk_manager.baocunYouxiShuju) {
//                         ailin_sdk_manager.baocunYouxiShuju(r, a);
//                     }
//                 }
//                 if (ailin_sdk_manager.beifenYouxiShuju) {
//                     ailin_sdk_manager.beifenYouxiShuju({
//                         callFun: function (e) {
//                             200 == e.code
//                                 ? console.log("forceBackup 提交成功：" + JSON.stringify(e))
//                                 : console.log("forceBackup 提交失败：" + JSON.stringify(e));
//                         }
//                     });
//                 }
//             } else {
//                 console.log("===不满足存档条件", t, n);
//             }
//         })({
//             term: function () {
//                 return (
//                     AL_GAME_HUA_GameData.GameData.getInstance().huoqu_yijiesuo_guanqia() >
//                     AL_GAME_HUA_GameConst.GameConst.getInstance().backup_level
//                 );
//             },
//             keys: y()
//         });
//     };
//     e.huoqu_suoyou_key = y;
//     e.huoqu_suoyou_cundang_shuju = m;
//     e.qingchu_cundang = function () {
//         ailin_sdk_manager.baocunYouxiShuju(ailin.Key_Gold, 0);
//         var e = 0;
//         for (var t = m(); e < t.length; e++) {
//             var o = t[e];
//             o.reset();
//             var i = 0;
//             for (var r = o.huoqu_yuancheng_key_list(); i < r.length; i++) {
//                 var a = r[i];
//                 ailin_sdk_manager.baocunYouxiShuju(a, localStorage.getItem(a));
//             }
//         }
//         return n(!0).then(function () {
//             localStorage.clear();
//             console.log("存档已清除");
//         });
//     };
// })(exports.ServerBackup || (exports.ServerBackup = {}));

exports.ServerBackup = void 0;

// 引入外部模块
const GameConst = require("./AL_GAME_HUA_GameConst");
const BankData = require("./AL_GAME_HUA_BankData");
const CurrencyData = require("./AL_GAME_HUA_CurrencyData");
const DateData = require("./AL_GAME_HUA_DateData");
const EmailData = require("./AL_GAME_HUA_EmailData");
const GameData = require("./AL_GAME_HUA_GameData");
const MissionData = require("./AL_GAME_HUA_MissionData");
const OnlineGiftData = require("./AL_GAME_HUA_OnlineGiftData");
const PayData = require("./AL_GAME_HUA_PayData");
const PlayerInfo = require("./AL_GAME_HUA_PlayerInfo");
const ShopData = require("./AL_GAME_HUA_ShopData");
const WelfareData = require("./AL_GAME_HUA_WelfareData");
const Define = require("./AL_GAME_HUA_Define");
const Instance = require("./AL_GAME_HUA_Instance");

/**
 * 检查是否需要提交存档
 * @returns {boolean} 是否需要提交存档
 */
function jiancha_shifou_xuyao_tijiao_cundang() {
    const unlockLevel = GameData.GameData.getInstance().huoqu_yijiesuo_guanqia();
    const backupLevel = GameConst.GameConst.getInstance().backup_level;
    const backupSpace = GameConst.GameConst.getInstance().backup_space;
    
    console.log(unlockLevel, backupLevel, backupSpace);
    
    if (unlockLevel > backupLevel) {
        const levelType = Instance.Instance.LevelType;
        
        if (levelType == Define.Define.LevelType.NORMAL) {
            return (unlockLevel - 1 - backupLevel) % backupSpace === 0;
        } else if (levelType == Define.Define.LevelType.SPECIAL) {
            const unlockSpecialLevel = GameData.GameData.getInstance().huoqu_yijiesuo_teshu_guanqia();
            return (unlockSpecialLevel - 1) % backupSpace === 0;
        }
    } else {
        console.log("===jiancha_shifou_xuyao_tijiao_cundang:", "level <= backupLevel", unlockLevel, backupLevel);
        return false;
    }
}

/**
 * 尝试提交存档
 * @param {boolean} force 是否强制提交
 * @returns {Promise} 一个Promise对象，表示存档提交过程的完成
 */
function zhixing_shifou_tijiao_cundang(force = false) {
    return new Promise((resolve, reject) => {
        resolve();
        if ((force || jiancha_shifou_xuyao_tijiao_cundang()) && ailin_sdk_manager.beifenYouxiShuju) {
            ailin_sdk_manager.beifenYouxiShuju({
                callFun: response => {
                    if (response.code === 200) {
                        console.log("===提交成功：" + JSON.stringify(response));
                        resolve();
                    } else {
                        console.log("===提交失败：" + JSON.stringify(response));
                        reject();
                    }
                }
            });
        }
    });
}

/**
 * 获取所有远程存档相关的键
 * @returns {Array} 所有相关的键
 */
function huoqu_suoyou_key() {
    let keys = [];
    const dataInstances = huoqu_suoyou_cundang_shuju();
    
    dataInstances.forEach(instance => {
        keys = [...keys, ...instance.huoqu_yuancheng_key_list()];
    });
    
    return [ailin.Key_Gold, ...keys];
}

/**
 * 获取所有数据存储对象的实例
 * @returns {Array} 所有数据存储对象的实例
 */
function huoqu_suoyou_cundang_shuju() {
    return [
        BankData.BankData.getInstance(),
        CurrencyData.CurrencyData.getInstance(),
        DateData.DateData.getInstance(),
        EmailData.EmailData.getInstance(),
        GameData.GameData.getInstance(),
        MissionData.MissionData.getInstance(),
        OnlineGiftData.OnlineGiftData.getInstance(),
        PayData.PayData.getInstance(),
        PlayerInfo.PlayerInfo.getInstance(),
        ShopData.ShopData.getInstance(),
        WelfareData.WelfareData.getInstance()
    ];
}

/**
 * 执行所有的存档操作
 */
function zhixing_suoyou_cundang_de_caozuo() {
    const backupHandler = {
        term: () => (
            GameData.GameData.getInstance().huoqu_yijiesuo_guanqia() > 
            GameConst.GameConst.getInstance().backup_level
        ),
        keys: huoqu_suoyou_key()
    };
    
    const onlineDataVersion = user_default.get_int_for_key("al_Online_Data_Version");
    if (onlineDataVersion === 0 && backupHandler.term()) {
        backupHandler.keys.forEach(key => {
            const storageItem = cc.sys.localStorage.getItem(key);
            if (typeof storageItem === "string" && ailin_sdk_manager.baocunYouxiShuju) {
                ailin_sdk_manager.baocunYouxiShuju(key, storageItem);
            }
        });
        
        if (ailin_sdk_manager.beifenYouxiShuju) {
            ailin_sdk_manager.beifenYouxiShuju({
                callFun: response => {
                    const message = response.code === 200 
                        ? "forceBackup 提交成功：" 
                        : "forceBackup 提交失败：";
                    console.log(message + JSON.stringify(response));
                }
            });
        }
    } else {
        console.log("===不满足存档条件", onlineDataVersion, backupHandler.term());
    }
}

/**
 * 清除存档
 * @returns {Promise} 完成后的Promise对象
 */
function qingchu_cundang() {
    ailin_sdk_manager.baocunYouxiShuju(ailin.Key_Gold, 0);
    
    const dataInstances = huoqu_suoyou_cundang_shuju();
    dataInstances.forEach(instance => {
        instance.reset();
        instance.huoqu_yuancheng_key_list().forEach(key => {
            ailin_sdk_manager.baocunYouxiShuju(key, localStorage.getItem(key));
        });
    });
    
    return zhixing_shifou_tijiao_cundang(true).then(() => {
        localStorage.clear();
        console.log("存档已清除");
    });
}

// 公开接口
exports.ServerBackup = {
    jiancha_shifou_xuyao_tijiao_cundang,
    zhixing_shifou_tijiao_cundang,
    zhixing_suoyou_cundang_de_caozuo,
    huoqu_suoyou_key,
    huoqu_suoyou_cundang_shuju,
    qingchu_cundang
};